﻿using Android.OS;
using Android.Views.InputMethods;
using Android.Widget;
using System;
using Xamarin.Forms;

namespace Moven.Droid.Renderers
{
	public static class KeyboardManager
	{
		//
		// Static Methods
		//
		internal static void HideKeyboard(this Android.Views.View inputView, bool overrideValidation = false)
		{
			using (InputMethodManager inputMethodManager = (InputMethodManager)Forms.Context.GetSystemService("input_method"))
			{
				if (!overrideValidation && !(inputView is EditText) && !(inputView is TextView) && !(inputView is SearchView))
				{
					throw new ArgumentException("inputView should be of type EditText, SearchView, or TextView");
				}
				IBinder windowToken = inputView.WindowToken;
				if (windowToken != null)
				{
					inputMethodManager.HideSoftInputFromWindow(windowToken, HideSoftInputFlags.None);
				}
			}
		}

		internal static void ShowKeyboard(this Android.Views.View inputView)
		{
			using (InputMethodManager inputMethodManager = (InputMethodManager)Forms.Context.GetSystemService("input_method"))
			{
				if (!(inputView is EditText) && !(inputView is TextView) && !(inputView is SearchView))
				{
					throw new ArgumentException("inputView should be of type EditText, SearchView, or TextView");
				}
				inputMethodManager.ShowSoftInput(inputView, ShowFlags.Forced);
				inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
			}
		}
	}
}
